package cn.jdy.plugins.service;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.jdy.framework.db.hibernate.BaseDao;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.platform.service.ConfService;
import cn.jdy.plugins.entity.Live;
import cn.jdy.plugins.entity.Maid;

/**
 * @title 佣金管理后台操作
 * @author jga
 * @since 2018-10-11
 */
@Service
public class MaidService extends BaseDao<Maid>{

	@Autowired
	private ConfService confService;
	
	/**
	 * 按条件查询
	 * @param sp
	 * 
	 * @return
	 */
	public List<Maid> query(Maid sp){
		if(sp==null) return super.findAll();
		if(sp.getiDisplayLength() > 0){
	     return super.find(createFilter(sp),sp.getiDisplayStart(),sp.getiDisplayLength());
			}
		return super.find(createFilter(sp));
	}
	 public List<Maid> querypage(Maid sp){
	       return super.find(createFilter(sp),sp.getPageNo(),18);
	  }
	public Long count(Maid sp){
		return super.count(createFilter(sp));
	}
	/**
	 * 拼装条件
	 * @param sp
	 * 
	 * @return
	 */
	private DetachedCriteria createFilter(Maid sp){
		DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
		if(!StringUtil.isNullOrEmpty(sp.getProductxid())) {
	        dc.add(Restrictions.like("productxid", sp.getProductxid()));
		} 
		
		if(!StringUtil.isNullOrEmpty(sp.getProductxid())) {
	        dc.add(Restrictions.eq("mch_id", sp.getMch_id()));
		} 
		
		if(!StringUtil.isNullOrEmpty(sp.getWermemberid())) {
	        dc.add(Restrictions.eq("wermemberid", sp.getWermemberid()));
		} 
		
		if(!StringUtil.isNullOrEmpty(sp.getCreateTime())) {
	        dc.add(Restrictions.like("createTime", sp.getCreateTime()));
	 
		} 
		dc.addOrder(Order.desc("createTime"));
			return dc;
	}
	
	/**
	 *查询本月订单
	 * @param sql
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Maid> querybenyuemai(String time,String mid) {
	List<Maid> list = new ArrayList<Maid>();
	try {
		String sql="SELECT  *  FROM Maid where mch_id=? and isfa=0 and STR_TO_DATE(createTime,'%Y-%m')=? " ;
				
	    SQLQuery sqlQuery = em().createSQLQuery(sql).addEntity(entityClass);;
	      sqlQuery.setParameter(0,mid);
	      sqlQuery.setParameter(1,time);
		list = sqlQuery.list();
	} catch (Exception e) {
		BaseLog.e(this.getClass(), "查询异常", e);
	}
	return list;
	} 	 

 
}
